package cn.imu.appdevexperiment2020._3activity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020._3activity.bean.News;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-11-13 14:17
 */
public class NewsTitleFragment extends Fragment {
   private boolean isTwoPane;

   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

      View view = inflater.inflate(R.layout.news_title_frag,container,false);

      RecyclerView newsTitleRecyclerView =(RecyclerView) view.findViewById(R.id.news_title_recycler_view);

      LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

      newsTitleRecyclerView.setLayoutManager(linearLayoutManager);
      NewsAdapter adapter = new NewsAdapter(getNews());
      newsTitleRecyclerView.setAdapter(adapter);
      return view;

   }

   private List<News> getNews() {
      List<News> newsList = new ArrayList<>();
      for (int i = 1; i <50 ; i++) {
         News news = new News();
         news.setTitle("这是标题 " + i);
         news.setContent(getRandomLengthContent("这是内容 " +i+"."));
         newsList.add(news);
      }
      return newsList;
   }

   private String getRandomLengthContent(String s) {
      Random random = new Random();
      int length =random.nextInt(20)+1;
      StringBuilder builder = new StringBuilder();
      for (int i = 0; i < length; i++) {
         builder.append(s);

      }
      return builder.toString();
   }

   @Override
   public void onActivityCreated(@Nullable Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      if (getActivity().findViewById(R.id.news_content_layout)!=null){
         isTwoPane=true;
      }else{
         isTwoPane=false;
      }
   }

   class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{

      private List<News> mNewsList;

      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

         View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item,viewGroup,false);

         final ViewHolder holder=new ViewHolder(view);
         view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               News news = mNewsList.get(holder.getAdapterPosition());
              Log.e("cch1",news.toString());
               if (isTwoPane){
                  NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
                  newsContentFragment.refresh(news.getTitle(),news.getContent());
               }else {
                  NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
               }
            }
         });



         return holder;
      }

      @Override
      public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
         News news = mNewsList.get(i);
         viewHolder.newsTitleText.setText(news.getTitle());
      }

      @Override
      public int getItemCount() {
         return mNewsList.size();
      }

      class ViewHolder extends RecyclerView.ViewHolder{
         TextView newsTitleText;

         public ViewHolder(@NonNull View itemView) {
            super(itemView);
            newsTitleText=(TextView) itemView.findViewById(R.id.news_title);
         }
      }
      public NewsAdapter(List<News> newsList) {
         mNewsList=newsList;
      }
   }
}
